package com.huika.renrench.control.investment.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.huika.lib.ui.widget.ToastMsg;
import com.huika.lib.utils.MoneyShowTool;
import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.support.bean.InvestmentPoolBean;
import com.huika.renrench.view.TitleBarHelper;

public class EarningsCalculateActivity extends BaseAct {

	private EditText invest_money_detail;
	private TextView earnings_detail;
	private Button confirm;
	private TextView earnings_day_detail;
	private TextView earnings_month_detail;
	private TextView earnings_year_detail;
	private InvestmentPoolBean bean;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_earnings_calculator);
	}
	
	@Override
	protected void initData() {
		super.initData();
		TitleBarHelper barHelper = new TitleBarHelper(this, -1);
		barHelper.setLeftMsg("投资");
		barHelper.setTitleMsg("收益计算器");
		bean = (InvestmentPoolBean) getIntent().getSerializableExtra("bean");
	}
	
	class MyWatcher implements TextWatcher {
		String tempStr;
		
		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			tempStr = s.toString();
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if(!checkInputMoney(s.toString())) {
				invest_money_detail.setText(tempStr);
				invest_money_detail.setSelection(invest_money_detail.length());
				return;
			} else if(s.length() > 1 && s.toString().substring(0, 1).equals("0") && !s.toString().startsWith("0.")) {
				invest_money_detail.setText(s.toString().substring(1));
				invest_money_detail.setSelection(invest_money_detail.length());
			}
		}
		
	}
	
	private boolean checkInputMoney(String money) {
		String patten = "^[0-9]{0,9}+(([.]{1})+([0-9]{0,2}))?$";
		return money.equals("")?true:money.matches(patten);
	}
	/**
	 * 
	 * @description：判断是否可以提交的金额
	 * @author zc
	 * @date 2015年6月23日 下午3:11:14
	 */
	private boolean canSubmitMoney(String money) {
		String patten = "^[0-9]+(.[0-9]{1,2})?$";
		return money.matches(patten);
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		invest_money_detail = (EditText) findViewById(R.id.invest_money_detail);
		earnings_detail = (TextView) findViewById(R.id.earnings_detail);
		confirm = (Button) findViewById(R.id.confirm);
		earnings_day_detail = (TextView) findViewById(R.id.earnings_day_detail);
		earnings_year_detail = (TextView) findViewById(R.id.earnings_year_detail);
		earnings_month_detail = (TextView) findViewById(R.id.earnings_month_detail);
		
		invest_money_detail.addTextChangedListener(new MyWatcher());
		earnings_detail.setText(MoneyShowTool.twolastDF(Double.parseDouble(bean.getYearRate()) * 100) + "%");
		confirm.setOnClickListener(this);
	}
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.confirm:
				if(check())
					calculate(Double.parseDouble(bean.getYearRate()));
				break;

		}
	}
	
	private boolean check() {
		if(invest_money_detail.length() == 0) {
			ToastMsg.showToastMsg(this, "请输入投资金额");
			return false;
		} else if(!canSubmitMoney(invest_money_detail.getText().toString())) {
			ToastMsg.showToastMsg(this, "您输入的格式不正确，请重新输入");
			invest_money_detail.setText("");
			return false;
		}
		return true;
	}
	
	private void calculate(double yearOfEarnings) {
		double money = Float.parseFloat(invest_money_detail.getText().toString());
		earnings_day_detail.setText(MoneyShowTool.twolastDF((money * yearOfEarnings) / 365) + "元");
		earnings_year_detail.setText(MoneyShowTool.twolastDF(money * yearOfEarnings)  + "元");
		earnings_month_detail.setText(MoneyShowTool.twolastDF((money * yearOfEarnings) / 12)  + "元");
	}

}
